package com.tang.consumer.config;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * @author Tang
 * @classname RpcBeanFactory
 * @description [  ]
 * @date 2021/1/24 17:37
 */
public class RpcBeanFactory<T> implements FactoryBean<T> {

    private Class<T> tClass;

    private RpcProxy<T> rpcProxy;

    public RpcBeanFactory(Class<T> tClass) {
        this.tClass = tClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public T getObject() throws Exception {
        return (T) Proxy.newProxyInstance(tClass.getClassLoader(),new Class[]{tClass}, rpcProxy);
    }

    @Override
    public Class<?> getObjectType() {
        return tClass;
    }

    public void setRpcProxy(RpcProxy<T> rpcProxy) {
        this.rpcProxy = rpcProxy;
    }
}